package org.egedede.osgi.guishell;

import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.JTree;

import org.egedede.osgi.guishell.actions.InstallAction;
import org.egedede.osgi.guishell.actions.StartBundleAction;
import org.egedede.osgi.guishell.actions.StopBundleAction;
import org.egedede.osgi.guishell.actions.UninstallBundleAction;
import org.egedede.osgi.guishell.model.Manager;

public class UIBuilder {
	private static StartBundleAction startAction;
	private static StopBundleAction stopAction;
	private static InstallAction installAction;
	private static UninstallBundleAction uninstallAction;
	private static BundleTreeCellListener bundleTreeCellListener;

	private UIBuilder(){};

	public static void init(Manager manager){
		installAction = new InstallAction();
		installAction.setInstallCommand(manager.getCommands().get("Install"));
		startAction = new StartBundleAction();
		startAction.setStartCommand(manager.getCommands().get("Start"));
		startAction.setManager(manager);
		stopAction = new StopBundleAction();
		stopAction.setStopCommand(manager.getCommands().get("Stop"));
		stopAction.setManager(manager);
		uninstallAction = new UninstallBundleAction();
		uninstallAction.setUninstallCommand(manager.getCommands().get("Uninstall"));
		uninstallAction.setManager(manager);

		bundleTreeCellListener = new BundleTreeCellListener();
		bundleTreeCellListener.setPopup(getBundlePopupMenu());
	}


	public static JMenu getMainMenu(){
		JMenu menu = new JMenu("Main");
		menu.add(installAction);
		menu.add(startAction);
		menu.add(stopAction);
		menu.add(uninstallAction);

		return menu;
	}
	public static JPopupMenu getBundlePopupMenu(){
		JPopupMenu menu = new JPopupMenu("Bundle Popup");
		menu.add(installAction);
		menu.add(startAction);
		menu.add(stopAction);
		menu.add(uninstallAction);

		return menu;
	}

	public static BundleTreeCellListener getBundleTreeCellListener(JTree tree){
		bundleTreeCellListener.setTree(tree);
		return bundleTreeCellListener;
	}
}
